package cn.itcast.crm.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;
import cn.itcast.crm.service.CustomerServcie;
//服务的实现类
@Service
@Transactional
public class CustomerServiceImpl implements CustomerServcie {
	//注入Dao
	@Autowired
	private CustomerRepository customerRepository;
//查询所有未关联 的  客户
	@Override
	public List<Customer> findNoAssociationCustomer() {
		
		return customerRepository.findByFixedAreaIdIsNull();
	}
//查询所有已关联的  客户
	@Override
	public List<Customer> findHasAssociationFixedAreaCustomers(
			String fixedareaid) {
		return customerRepository.findByFixedAreaId(fixedareaid);
	}
//将客户关联到指定 定区
	@Override
	public void associationCustomersToFixedArea(String customerIdStr,
			String fixedAreaId) {
		//修复  bug:实现 已关联 --->未关联动作   即:添加一个 解除关联动作
		customerRepository.clearFixedAreaId(fixedAreaId);
		
		//避免前台传 空的字符串,造成空指针异常 ,做以下判断
		if(StringUtils.isBlank(customerIdStr)||"null".equalsIgnoreCase(customerIdStr)){
			return;
		}
		
		
		//切割字符串 
		String[] customerIdArray =customerIdStr.split(",");
		for (String idStr : customerIdArray) {
			Integer id = Integer.parseInt(idStr);//For input string: "null"
			customerRepository.updateFixedAreaId(fixedAreaId,id);
		}

	}
	//前台页面   用户注册
	@Override
	public void regist(Customer customer) {
		System.out.println(customer);
		customerRepository.save(customer);
	}
	//根据电话号码查询用户
	@Override
	public Customer findByTelephone(String telephone) {
		return customerRepository.findByTelephone(telephone);
	}
	//修改客户信息
	@Override
	public void updateType(String telephone) {
		customerRepository.updateType(telephone);
	}

}

















